//
//  LoginViewController.m
//  Slice
//
//  Created by lanou on 16/6/21.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "LoginViewController.h"
#import "LeftViewController.h"

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userNmeTF;

@property (weak, nonatomic) IBOutlet UITextField *passWordTF;





@end

@implementation LoginViewController
//登陆按钮
- (IBAction)loginAction:(id)sender {
//    请求登陆借口
    [RequestTool requsetWithType:POST URLString:LOGIN_URL paremeter:@{@"email":self.userNmeTF.text,@"passwd":self.passWordTF.text} callBack:^(NSData *data, NSError *error) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves | NSJSONReadingMutableContainers error:nil];
        
        NSLog(@"%@",dic);
        
        NSNumber *result = dic[@"result"];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (result.intValue == 1) {
                
                [[NSUserDefaults standardUserDefaults]setObject:dic[@"data"] forKey:@"user"];
                [[NSUserDefaults standardUserDefaults] synchronize];
        
                [self dismissViewControllerAnimated:YES completion:nil];
            } else {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登陆失败" message:dic[@"data"][@"msg"] preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:nil];
                [alert addAction:action];
                [self presentViewController:alert animated:YES completion:nil];
            }
        });
    
    }];
}
//返回按钮
- (IBAction)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"321"]) {
        RegisterViewController *vc = segue.destinationViewController;
        vc.registerValue = ^(NSString *str1, NSString *str2) {
            self.userNmeTF.text = str1;
            self.passWordTF.text = str2;
            
        };
    }
}


@end
